package wangjg.coco.common;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class BingTranslator {

	public String en2cn(String text){
		try {
			text = URLEncoder.encode(text,"utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		String url = "http://api.microsofttranslator.com/V2/Ajax.svc/Translate?appId=A4D660A48A6A97CCA791C34935E4C02BBB1BEC1C&from=en&to=zh-cn&text="+text;
		return this.loadHTML(url);
	}
	
	public String cn2en(String text){
		try {
			text = URLEncoder.encode(text,"utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		String url = "http://api.microsofttranslator.com/V2/Ajax.svc/Translate?appId=A4D660A48A6A97CCA791C34935E4C02BBB1BEC1C&from=zh-cn&to=en&text="+text;
		return this.loadHTML(url);
	}
	
	private String loadHTML(String url){
		HttpRequest req = new HttpRequest(url);
		return req.load();
	}

	public static void main(String[] args) {
		String s=new BingTranslator().en2cn("Prints;Object");
		System.out.println(s);
	}
}
